package com.neueedu.android.concat;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.neueedu.android.R;

import java.util.ArrayList;
import java.util.List;

public class ConcatAddActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_concat_add);
        Button btnQueryConcat = findViewById(R.id.btnQueryConcat);



        btnQueryConcat.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                //判断是否申请了权限
//
//                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//                        if ((this.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) ||
//                                (this.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
//                            this.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
//                        } else {
//                            Log.d(TAG, "已有权限,不需要再申请");
//                            mHandler.sendEmptyMessage(MyHandler.PERMISSION_ALLOW);
//                        }
//
//                        return;
//                    } else {
//                        mHandler.sendEmptyMessage(MyHandler.PERMISSION_ALLOW);
//                    }

                showConcat();








            }
        });


    }


    private void showConcat(){
        ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,new String[] { "_id", "display_name" },null,null,null);

        List<String> list = new ArrayList<>();

        while (cursor.moveToNext()){
            String _id= cursor.getString(0);
            String name = cursor.getString(1);


            Cursor c2 =     cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+" = ?", new String[] { _id  }, null);  // 传入联系人_id
            if(c2.moveToNext()){
                name += "-"+ c2.getString(c2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            }
            list.add(name);
        }


        final  String[] names = list.toArray(new String[0]);
//
//        final String[] names =new String[list.size()];
//        for (int i = 0; i < list.size(); i++) {
//            names[i] =  list.get(i);
//        }



        AlertDialog dialog = new AlertDialog.Builder(ConcatAddActivity.this)
                .setTitle("请选择联系人")
                .setIcon(android.R.drawable.ic_dialog_info)
                .setSingleChoiceItems(names, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.d("Content", names[which]);
                    }
                })
//                        .setNeutralButton()
                .setPositiveButton("取消",null).show();
    }



//    @Override
//    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//
//        showConcat();
//    }


}
